/* em/main.c 
 * 
 * This file is part of em. 
 * 
 * em is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * em is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with em. If not, see <https://www.gnu.org/licenses/>
 */ 




#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <em/em.h>

/* em, is a macro-processor which gives the user more free. 
 * #m macro-name (left-mspec, right-mspec) 
 * 	macro-body
 * #em
 *
 * left-mspec and right-mspec specifics the type of the words 
 * on the left side and on the right side. 
 *
 * mspec: [direct][nr][type]
 *
 * direct: 	+ 	>
 * 		= 	=
 * 		- 	<
 * 		? 	any
 *
 * nr: 		number
 * type: 	s 	string, string with '""'
 * 		i 	digit
 * 		n 	name, string without '""'
 * 		e 	any
 *
 * macro-body:
 * 		*nr 	directly output the nr'th value after / before
 * */


static int usage(const char *s) {
	fprintf(stderr, "Usage: %s input-file\n", s);

	return 1;
}


int main(int argc, const char **argv) {
	int 	fd;
	size_t 	size;
	char 	*p;

	(void) argc;

	if (!argv[1]) 
		return usage(argv[0]);

	if (unlikely(((fd = open(argv[1], O_RDONLY)) < 0))) {
		perror(argv[0]);

		return 1;
	}

	if (unlikely((p = mmap(NULL, (size = lseek(fd, 0, SEEK_END)), PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)) {
		perror(argv[0]);

		return 1;
	}

	scan(p, size);
	check_macros();
	expand();

	return 0;
}

